```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高性能分布式限流系统设计 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-top: 2rem;
            margin-bottom: 1rem;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
            color: white;
            padding: 6rem 0;
            position: relative;
            overflow: hidden;
        }
        .hero-content {
            position: relative;
            z-index: 2;
        }
        .hero-title {
            font-size: 3.5rem;
            font-weight: 700;
            line-height: 1.2;
            margin-bottom: 1.5rem;
            text-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .hero-subtitle {
            font-size: 1.5rem;
            font-weight: 300;
            max-width: 700px;
            margin: 0 auto 2rem;
            opacity: 0.9;
        }
        .card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 20px rgba(0,0,0,0.08);
            margin-bottom: 2rem;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.12);
        }
        .card-header {
            padding: 1.5rem;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            font-weight: 600;
            display: flex;
            align-items: center;
        }
        .card-header i {
            margin-right: 10px;
            font-size: 1.2rem;
        }
        .card-body {
            padding: 1.5rem;
        }
        .feature-icon {
            font-size: 2.5rem;
            color: #6e8efb;
            margin-bottom: 1rem;
        }
        .footer {
            background-color: #1a1a1a;
            color: #fff;
            padding: 3rem 0;
            margin-top: 4rem;
        }
        .footer a {
            color: #fff;
            transition: color 0.3s;
        }
        .footer a:hover {
            color: #6e8efb;
            text-decoration: none;
        }
        .diagram-container {
            background: white;
            border-radius: 8px;
            padding: 1rem;
            margin: 2rem 0;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        .highlight-box {
            background: rgba(110, 142, 251, 0.1);
            border-left: 4px solid #6e8efb;
            padding: 1.5rem;
            margin: 2rem 0;
            border-radius: 0 8px 8px 0;
        }
        .pros-cons {
            display: flex;
            margin: 1.5rem 0;
        }
        .pros, .cons {
            flex: 1;
            padding: 1rem;
            border-radius: 8px;
        }
        .pros {
            background: rgba(76, 175, 80, 0.1);
            border-left: 4px solid #4CAF50;
            margin-right: 0.5rem;
        }
        .cons {
            background: rgba(244, 67, 54, 0.1);
            border-left: 4px solid #F44336;
            margin-left: 0.5rem;
        }
        .tech-card {
            border: 1px solid rgba(0,0,0,0.1);
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1rem;
            transition: all 0.3s;
        }
        .tech-card:hover {
            border-color: #6e8efb;
            box-shadow: 0 5px 15px rgba(110, 142, 251, 0.1);
        }
        .tech-card h4 {
            display: flex;
            align-items: center;
            margin-bottom: 0.5rem;
        }
        .tech-card i {
            margin-right: 8px;
            color: #6e8efb;
        }
        .image-container {
            text-align: center;
            margin: 2rem 0;
        }
        .image-container img {
            max-width: 100%;
            border-radius: 8px;
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        @media (max-width: 768px) {
            .hero-title {
                font-size: 2.5rem;
            }
            .hero-subtitle {
                font-size: 1.2rem;
            }
            .pros-cons {
                flex-direction: column;
            }
            .pros, .cons {
                margin: 0.5rem 0;
            }
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-center">
        <div class="container mx-auto px-4 hero-content">
            <h1 class="hero-title">高性能分布式限流系统设计</h1>
            <p class="hero-subtitle">在现代分布式系统中构建稳定、高效的流量控制机制</p>
            <div class="flex justify-center space-x-4">
                <a href="#introduction" class="px-6 py-3 bg-white text-indigo-600 rounded-full font-medium hover:bg-gray-100 transition">开始阅读</a>
            </div>
        </div>
        <div class="absolute inset-0 opacity-20" style="background-image: url('https://images.unsplash.com/photo-1551288049-bebda4e38f71?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80'); background-size: cover; background-position: center;"></div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-4 py-12 max-w-5xl">
        <!-- Introduction Section -->
        <section id="introduction" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">设计一个高性能的分布式限流系统</h2>
            <p class="text-lg mb-6">在现代分布式系统中，流量控制是保障系统稳定性和高可用性的关键技术之一。随着用户规模的增长和业务需求的复杂化，系统往往需要面对突发流量或恶意攻击等场景，这就对限流机制提出了更高的要求。一套高性能的分布式限流系统，不仅能够精准控制流量，防止系统过载，还需要在高并发场景下具备低延迟、高吞吐的能力，同时满足多样化的业务需求。</p>
            
            <div class="image-container">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1732943915818-3e9179a0-cf2e-428b-a591-4f8495c4db55.png" alt="分布式限流系统示意图">
            </div>
            
            <p class="mb-4">从需求出发，我们需要明确限流的目标，例如按用户、接口或IP维度限制访问频率，确保系统资源的公平使用。其次，选择合适的限流算法至关重要，例如固定窗口计数、滑动窗口、令牌桶或漏桶算法，它们各有优缺点，需要结合业务场景选择合适的方案。</p>
            <p>同时，分布式场景中的限流不同于单机限流，需要应对多节点协同的问题，这就涉及到分布式存储和一致性方案的设计，比如借助 Redis 或 ZooKeeper 实现全局限流。</p>
        </section>

        <!-- Requirements Analysis Section -->
        <section id="requirements" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">需求分析</h2>
            <p class="text-lg mb-6">设计一个高性能的分布式限流系统，需求分析是关键的起点，它直接决定了系统的功能范围、实现方案以及性能要求。</p>
            
            <div class="image-container">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1732943948168-331b54c1-c39e-4ec8-b534-3d553e856afb.png" alt="需求分析示意图">
            </div>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-indigo-50 text-indigo-600">
                        <i class="fas fa-bullseye"></i> 限流目标
                    </div>
                    <div class="card-body">
                        <ul class="space-y-2">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> <strong>保护系统稳定性</strong>：避免突发流量（如流量洪峰或恶意攻击）导致系统过载、服务不可用</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> <strong>优化资源利用</strong>：通过限流保证公平分配资源，避免某些用户或接口占用过多系统资源</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> <strong>提升用户体验</strong>：在限流情况下，提供平滑的降级策略，防止用户体验断崖式下滑</li>
                        </ul>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-indigo-50 text-indigo-600">
                        <i class="fas fa-tasks"></i> 应用场景
                    </div>
                    <div class="card-body">
                        <ul class="space-y-2">
                            <li><i class="fas fa-code text-blue-500 mr-2"></i> 接口调用限制</li>
                            <li><i class="fas fa-users text-blue-500 mr-2"></i> 用户行为控制</li>
                            <li><i class="fas fa-shopping-cart text-blue-500 mr-2"></i> 业务场景保护</li>
                            <li><i class="fas fa-network-wired text-blue-500 mr-2"></i> 分布式服务协调</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="highlight-box">
                <h3 class="text-xl font-semibold mb-3 text-indigo-600">性能要求</h3>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="flex items-start">
                        <i class="fas fa-bolt text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-medium">高并发支持</h4>
                            <p class="text-sm text-gray-600">承载数万甚至数十万的并发请求</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-stopwatch text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-medium">低延迟响应</h4>
                            <p class="text-sm text-gray-600">限流判断快速完成，避免成为系统瓶颈</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-expand text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-medium">扩展性强</h4>
                            <p class="text-sm text-gray-600">支持动态扩容，适应流量变化</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Algorithm Selection Section -->
        <section id="algorithms" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">限流算法选择</h2>
            <p class="text-lg mb-6">限流算法是分布式限流系统的核心，它直接决定了限流的精准性、性能以及适用场景。选择合适的限流算法需要根据业务需求、系统负载特性和性能指标综合考虑。</p>
            
            <div class="image-container">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1732943973421-d9f72323-e935-446b-b719-b60e7747f1d2.png" alt="限流算法示意图">
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <!-- 固定时间窗口算法 -->
                <div class="tech-card">
                    <h4><i class="fas fa-clock"></i> 固定时间窗口算法</h4>
                    <p class="text-sm text-gray-600 mb-3">将时间划分为固定的窗口（如1秒、1分钟），统计窗口内的请求数</p>
                    <div class="pros-cons">
                        <div class="pros">
                            <h5 class="font-medium text-green-600">优点</h5>
                            <ul class="text-sm">
                                <li>实现简单</li>
                                <li>内存占用小</li>
                            </ul>
                        </div>
                        <div class="cons">
                            <h5 class="font-medium text-red-600">缺点</h5>
                            <ul class="text-sm">
                                <li>存在突发流量问题</li>
                            </ul>
                        </div>
                    </div>
                </div>
                
                <!-- 滑动时间窗口算法 -->
                <div class="tech-card">
                    <h4><i class="fas fa-sliders-h"></i> 滑动时间窗口算法</h4>
                    <p class="text-sm text-gray-600 mb-3">通过滑动窗口动态统计请求数量实现更平滑的限流效果</p>
                    <div class="pros-cons">
                        <div class="pros">
                            <h5 class="font-medium text-green-600">优点</h5>
                            <ul class="text-sm">
                                <li>限流更精准</li>
                                <li>无突发流量问题</li>
                            </ul>
                        </div>
                        <div class="cons">
                            <h5 class="font-medium text-red-600">缺点</h5>
                            <ul class="text-sm">
                                <li>实现复杂</li>
                                <li>资源占用高</li>
                            </ul>
                        </div>
                    </div>
                </div>
                
                <!-- 令牌桶算法 -->
                <div class="tech-card">
                    <h4><i class="fas fa-coins"></i> 令牌桶算法</h4>
                    <p class="text-sm text-gray-600 mb-3">系统以固定速率向令牌桶中添加令牌，请求需要获取令牌才能通过</p>
                    <div class="pros-cons">
                        <div class="pros">
                            <h5 class="font-medium text-green-600">优点</h5>
                            <ul class="text-sm">
                                <li>支持突发流量</li>
                                <li>平滑控制</li>
                            </ul>
                        </div>
                        <div class="cons">
                            <h5 class="font-medium text-red-600">缺点</h5>
                            <ul class="text-sm">
                                <li>令牌管理复杂</li>
                            </ul>
                        </div>
                    </div>
                </div>
                
                <!-- 漏桶算法 -->
                <div class="tech-card">
                    <h4><i class="fas fa-tint"></i> 漏桶算法</h4>
                    <p class="text-sm text-gray-600 mb-3">请求被以固定速率处理，不允许突发流量超过预设容量</p>
                    <div class="pros-cons">
                        <div class="pros">
                            <h5 class="font-medium text-green-600">优点</h5>
                            <ul class="text-sm">
                                <li>流量稳定输出</li>
                                <li>避免下游冲击</li>
                            </ul>
                        </div>
                        <div class="cons">
                            <h5 class="font-medium text-red-600">缺点</h5>
                            <ul class="text-sm">
                                <li>不支持突发流量</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="highlight-box mt-6">
                <h3 class="text-xl font-semibold mb-3 text-indigo-600">选择算法的关键因素</h3>
                <div class="grid md:grid-cols-2 gap-4">
                    <div>
                        <h4 class="font-medium mb-2">业务因素</h4>
                        <ul class="text-sm space-y-1">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 流量特性（突发性、分布）</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 业务优先级</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2">技术因素</h4>
                        <ul class="text-sm space-y-1">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 实现复杂度</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 分布式需求</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Architecture Design Section -->
        <section id="architecture" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">分布式架构设计</h2>
            <p class="text-lg mb-6">设计一个高性能的分布式限流系统，需要从分布式架构的特点出发，解决限流过程中遇到的高并发、数据一致性、低延迟等问题。</p>
            
            <div class="card bg-white mb-8">
                <div class="card-header bg-indigo-50 text-indigo-600">
                    <i class="fas fa-sitemap"></i> 分布式限流的核心需求
                </div>
                <div class="card-body">
                    <div class="grid md:grid-cols-2 gap-4">
                        <ul class="space-y-2">
                            <li><i class="fas fa-globe text-indigo-500 mr-2"></i> 全局限流</li>
                            <li><i class="fas fa-server text-indigo-500 mr-2"></i> 节点级限流</li>
                            <li><i class="fas fa-sliders-h text-indigo-500 mr-2"></i> 动态配置</li>
                        </ul>
                        <ul class="space-y-2">
                            <li><i class="fas fa-shield-alt text-indigo-500 mr-2"></i> 高可用性</li>
                            <li><i class="fas fa-bolt text-indigo-500 mr-2"></i> 低延迟</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4">分布式限流的关键技术</h3>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-blue-50 text-blue-600">
                        <i class="fas fa-database"></i> 分布式存储
                    </div>
                    <div class="card-body">
                        <h4 class="font-medium mb-2">Redis</h4>
                        <p class="text-sm text-gray-600 mb-3">使用Lua脚本保证限流操作的原子性，性能高，支持数据持久化</p>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i> 适合：高并发计数器
                        </div>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-blue-50 text-blue-600">
                        <i class="fas fa-link"></i> 一致性协议
                    </div>
                    <div class="card-body">
                        <h4 class="font-medium mb-2">Raft/Paxos</h4>
                        <p class="text-sm text-gray-600 mb-3">用于分布式节点间的状态同步，保证限流策略的一致性</p>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i> 适合：高一致性场景
                        </div>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-blue-50 text-blue-600">
                        <i class="fas fa-project-diagram"></i> API 网关
                    </div>
                    <div class="card-body">
                        <h4 class="font-medium mb-2">Nginx/Spring Gateway</h4>
                        <p class="text-sm text-gray-600 mb-3">在网关层设置限流规则，预处理请求减少后端压力</p>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i> 适合：入口限流
                        </div>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4">典型的分布式限流架构实现</h3>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card bg-white">
                    <div class="card-header bg-purple-50 text-purple-600">
                        <i class="fas fa-network-wired"></i> 集中式限流
                    </div>
                    <div class="card-body">
                        <ul class="space-y-2 text-sm">
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 流量先经过网关或集中限流服务</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 便于管理和维护</li>
                            <li><i class="fas fa-times text-red-500 mr-2"></i> 可能成为性能瓶颈</li>
                        </ul>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-purple-50 text-purple-600">
                        <i class="fas fa-sitemap"></i> 分布式限流
                    </div>
                    <div class="card-body">
                        <ul class="space-y-2 text-sm">
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 各节点独立执行限流逻辑</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 高可用、低延迟</li>
                            <li><i class="fas fa-times text-red-500 mr-2"></i> 实现复杂</li>
                        </ul>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-purple-50 text-purple-600">
                        <i class="fas fa-blender-phone"></i> 混合式限流
                    </div>
                    <div class="card-body">
                        <ul class="space-y-2 text-sm">
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 网关层全局限流</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 服务节点局部限流</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i> 综合优势</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="mt-8">
                <h3 class="text-2xl font-semibold mb-4">优化策略</h3>
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="flex items-start p-4 bg-white rounded-lg shadow-sm">
                        <i class="fas fa-microchip text-indigo-500 mt-1 mr-4 text-xl"></i>
                        <div>
                            <h4 class="font-medium mb-1">局部限流优先</h4>
                            <p class="text-sm text-gray-600">请求优先在本地节点执行限流逻辑，减少分布式存储访问压力</p>
                        </div>
                    </div>
                    <div class="flex items-start p-4 bg-white rounded-lg shadow-sm">
                        <i class="fas fa-chart-line text-indigo-500 mt-1 mr-4 text-xl"></i>
                        <div>
                            <h4 class="font-medium mb-1">流量预估</h4>
                            <p class="text-sm text-gray-600">对历史流量数据进行建模，预测未来流量峰值</p>
                        </div>
                    </div>
                    <div class="flex items-start p-4 bg-white rounded-lg shadow-sm">
                        <i class="fas fa-layer-group text-indigo-500 mt-1 mr-4 text-xl"></i>
                        <div>
                            <h4 class="font-medium mb-1">层级限流</h4>
                            <p class="text-sm text-gray-600">在网关、应用服务、数据库等不同层级分别设置限流规则</p>
                        </div>
                    </div>
                    <div class="flex items-start p-4 bg-white rounded-lg shadow-sm">
                        <i class="fas fa-exclamation-triangle text-indigo-500 mt-1 mr-4 text-xl"></i>
                        <div>
                            <h4 class="font-medium mb-1">熔断与降级</h4>
                            <p class="text-sm text-gray-600">限流规则触发后，快速熔断异常请求，并提供降级服务</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Technology Selection Section -->
        <section id="technology" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">核心技术选型</h2>
            <p class="text-lg mb-6">在设计高性能分布式限流系统时，核心技术选型直接影响系统的性能、可用性和复杂度。技术选型需要结合业务需求、流量规模以及团队技术栈。</p>
            
            <h3 class="text-2xl font-semibold mb-4 mt-8">存储层选型</h3>
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-green-50 text-green-600">
                        <i class="fas fa-database"></i> Redis
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-3">高并发流量计数器、全局限流状态存储</p>
                        <div class="pros-cons">
                            <div class="pros">
                                <h5 class="font-medium text-green-600">Pros</h5>
                                <ul class="text-xs">
                                    <li>高性能，支持百万级QPS</li>
                                    <li>Lua脚本原子操作</li>
                                    <li>分布式集群支持</li>
                                </ul>
                            </div>
                            <div class="cons">
                                <h5 class="font-medium text-red-600">Cons</h5>
                                <ul class="text-xs">
                                    <li>单实例可能成为瓶颈</li>
                                    <li>超高并发写入压力</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-green-50 text-green-600">
                        <i class="fas fa-server"></i> MongoDB/Cassandra
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-3">大规模规则存储或历史请求记录</p>
                        <div class="pros-cons">
                            <div class="pros">
                                <h5 class="font-medium text-green-600">Pros</h5>
                                <ul class="text-xs">
                                    <li>分布式存储和查询</li>
                                    <li>扩展性强</li>
                                </ul>
                            </div>
                            <div class="cons">
                                <h5 class="font-medium text-red-600">Cons</h5>
                                <ul class="text-xs">
                                    <li>性能相对较低</li>
                                    <li>不适合实时计数</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4">通信与同步选型</h3>
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-yellow-50 text-yellow-600">
                        <i class="fas fa-cogs"></i> 配置中心
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Nacos, Apollo, Consul</p>
                        <p class="text-xs text-gray-500">限流规则的动态下发与更新</p>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-yellow-50 text-yellow-600">
                        <i class="fas fa-sync-alt"></i> 一致性协议
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Raft, Paxos, Zookeeper</p>
                        <p class="text-xs text-gray-500">限流计数器的分布式同步</p>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-yellow-50 text-yellow-600">
                        <i class="fas fa-stream"></i> 消息队列
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Kafka, RabbitMQ</p>
                        <p class="text-xs text-gray-500">流量削峰填谷，异步限流</p>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4">网关选型</h3>
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-red-50 text-red-600">
                        <i class="fas fa-cloud"></i> Spring Cloud Gateway
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">基于Spring生态的微服务限流</p>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-plus-circle text-green-500 mr-1"></i> 与Spring生态无缝集成
                        </div>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-minus-circle text-red-500 mr-1"></i> 性能不如专用网关
                        </div>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-red-50 text-red-600">
                        <i class="fas fa-server"></i> Nginx
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">流量入口的全局限流</p>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-plus-circle text-green-500 mr-1"></i> 成熟高效，支持Lua扩展
                        </div>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-minus-circle text-red-500 mr-1"></i> 复杂逻辑需后端实现
                        </div>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-red-50 text-red-600">
                        <i class="fas fa-network-wired"></i> Envoy
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Service Mesh中流量控制</p>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-plus-circle text-green-500 mr-1"></i> 轻量、高性能
                        </div>
                        <div class="flex items-center text-xs text-gray-500">
                            <i class="fas fa-minus-circle text-red-500 mr-1"></i> 配置复杂度高
                        </div>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4">本地优化技术选型</h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card bg-white">
                    <div class="card-header bg-blue-50 text-blue-600">
                        <i class="fas fa-memory"></i> 缓存技术
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Guava Cache, Caffeine</p>
                        <p class="text-xs text-gray-500">减少分布式存储的频繁访问</p>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-blue-50 text-blue-600">
                        <i class="fas fa-tasks"></i> 线程池管理
                    </div>
                    <div class="card-body">
                        <p class="text-sm text-gray-600 mb-2">Java线程池, Disruptor</p>
                        <p class="text-xs text-gray-500">流量整形、异步处理</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation & Optimization Section -->
        <section id="implementation" class="mb-16">
            <h2 class="text-3xl font-bold mb-6">系统实现与优化</h2>
            <p class="text-lg mb-6">构建一个高性能分布式限流系统，除了理论设计外，还需要注重实际的系统实现和运行中的性能优化。限流系统不仅要确保流量控制的精准性，还要尽量减少对业务系统性能的影响。</p>
            
            <h3 class="text-2xl font-semibold mb-4">系统实现</h3>
            
            <div class="card bg-white mb-6">
                <div class="card-header bg-gray-50 text-gray-600">
                    <i class="fas fa-layer-group"></i> 分层设计
                </div>
                <div class="card-body">
                    <div class="grid md:grid-cols-3 gap-4">
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">入口层限流</h4>
                            <p class="text-sm text-gray-600">在网关实现全局限流，第一层拦截</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">服务层限流</h4>
                            <p class="text-sm text-gray-600">接口或用户级细粒度限流</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">客户端限流</h4>
                            <p class="text-sm text-gray-600">前置校验减少服务端压力</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card bg-white mb-6">
                <div class="card-header bg-gray-50 text-gray-600">
                    <i class="fas fa-cogs"></i> 核心限流逻辑
                </div>
                <div class="card-body">
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2">滑动窗口限流</h4>
                            <p class="text-sm text-gray-600 mb-3">请求时间存储为有序集合，按窗口边界动态计算</p>
                            <div class="bg-gray-50 p-3 rounded text-xs font-mono">
                                <span class="text-blue-600">redis.zadd</span>(key, timestamp, timestamp)<br>
                                <span class="text-blue-600">redis.zremrangebyscore</span>(key, 0, current_time - window_size)<br>
                                <span class="text-blue-600">count</span> = redis.zcard(key)<br>
                                <span class="text-blue-600">if</span> count > threshold <span class="text-blue-600">then</span> reject
                            </div>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2">令牌桶算法</h4>
                            <p class="text-sm text-gray-600 mb-3">Lua脚本实现令牌生成与消费的原子操作</p>
                            <div class="bg-gray-50 p-3 rounded text-xs font-mono">
                                <span class="text-blue-600">local</span> tokens = redis.get(key)<br>
                                <span class="text-blue-600">local</span> last_time = redis.get(last_key)<br>
                                <span class="text-blue-600">local</span> now = os.time()<br>
                                <span class="text-blue-600">local</span> new_tokens = (now - last_time) * rate<br>
                                tokens = math.min(capacity, tokens + new_tokens)<br>
                                <span class="text-blue-600">if</span> tokens >= 1 <span class="text-blue-600">then</span><br>
                                &nbsp;&nbsp;redis.set(key, tokens - 1)<br>
                                &nbsp;&nbsp;redis.set(last_key, now)<br>
                                &nbsp;&nbsp;<span class="text-blue-600">return</span> true<br>
                                <span class="text-blue-600">else</span> <span class="text-blue-600">return</span> false
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4 mt-8">系统优化</h3>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="card bg-white">
                    <div class="card-header bg-teal-50 text-teal-600">
                        <i class="fas fa-tachometer-alt"></i> 性能优化
                    </div>
                    <div class="card-body">
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-database text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">缓存机制</h4>
                                    <p class="text-sm text-gray-600">使用本地缓存减少分布式存储访问</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-clone text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">批量操作</h4>
                                    <p class="text-sm text-gray-600">限流计数器批量写入降低频率</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-network-wired text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">减少网络开销</h4>
                                    <p class="text-sm text-gray-600">本地优先校验减少通信</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="card bg-white">
                    <div class="card-header bg-teal-50 text-teal-600">
                        <i class="fas fa-shield-alt"></i> 高可用优化
                    </div>
                    <div class="card-body">
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-sitemap text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">分布式存储高可用</h4>
                                    <p class="text-sm text-gray-600">Redis集群主从与哨兵机制</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-triangle text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">服务降级与熔断</h4>
                                    <p class="text-sm text-gray-600">限流系统故障时保障核心功能</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-copy text-teal-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-medium">规则冗余备份</h4>
                                    <p class="text-sm text-gray-600">配置中心不可用时启用本地规则</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="card bg-white">
                <div class="card-header bg-teal-50 text-teal-600">
                    <i class="fas fa-chart-pie"></i> 监控与预警
                </div>
                <div class="card-body">
                    <div class="grid md:grid-cols-3 gap-4">
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">实时监控</h4>
                            <p class="text-sm text-gray-600">Prometheus + Grafana可视化限流指标</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">日志分析</h4>
                            <p class="text-sm text-gray-600">ELK Stack记录限流日志</p>
                        </div>
                        <div>
                            <h4 class="font-medium text-indigo-600 mb-2">动态调整</h4>
                            <p class="text-sm text-gray-600">根据监控数据优化限流策略</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="highlight-box mt-8">
                <h3 class="text-xl font-semibold mb-3 text-indigo-600">优化后的优势</h3>
                <div class="grid md:grid-cols-4 gap-4 text-center">
                    <div>
                        <i class="fas fa-bolt text-yellow-500 text-2xl mb-2"></i>
                        <h4 class="font-medium">高性能</h4>
                        <p class="text-xs text-gray-600">处理海量并发请求</p>
                    </div>
                    <div>
                        <i class="fas fa-shield-alt text-green-500 text-2xl mb-2"></i>
                        <h4 class="font-medium">高可用性</h4>
                        <p class="text-xs text-gray-600">避免单点故障</p>
                    </div>
                    <div>
                        <i class="fas fa-expand-arrows-alt text-blue-500 text-2xl mb-2"></i>
                        <h4 class="font-medium">弹性与灵活性</h4>
                        <p class="text-xs text-gray-600">应对流量峰值</p>
                    </div>
                    <div>
                        <i class="fas fa-chart-line text-purple-500 text-2xl mb-2"></i>
                        <h4 class="font-medium">可观测性</h4>
                        <p class="text-xs text-gray-600">快速定位瓶颈</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Footer -->
        <footer class="footer">
            <div class="container mx-auto px-4 text-center">
                <h3 class="text-xl font-medium mb-2">技术小馆</h3>
                <p class="mb-4">探索技术之美，分享架构之道</p>
                <div class="text-sm">
                    <a href="http://www.yuque.com/jtostring" class="hover:text-indigo-300 transition">语雀地址: www.yuque.com/jtostring</a>
                </div>
            </div>
        </footer>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```